﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nb.Common.SystemDates;

namespace Nb.Common.Ioc
{
    [TestClass]
    public class NbServiceRegistryTest
    {
        [TestMethod]
        public void Default_Service_ShouldBeAService()
        {
            var xxxServices = XxxServiceRegistry.Instance.GetServices();
            Assert.AreEqual(1, xxxServices.Count);

            var xxxService = XxxServiceRegistry.Instance.GetService();
            Assert.AreEqual("AService", xxxService.GetName());
        }

        [TestMethod]
        public void ReplaceB_Default_Service_ShouldBeBService()
        {
            XxxServiceRegistry.Instance.ResetDefaultServiceFunc(() => new BService());
            var newServices = XxxServiceRegistry.Instance.GetServices();
            Assert.AreEqual(1, newServices.Count);
            var newService = XxxServiceRegistry.Instance.GetService();
            Assert.AreEqual("BService", newService.GetName());

            var newService2 = XxxServiceRegistry.Instance.GetService();
            Assert.AreNotSame(newService, newService2);

            //back to default
            XxxServiceRegistry.Instance.ResetDefaultServiceFunc(() => new AService());
        }



        public interface IXxxService
        {
            string GetName();
        }

        public class AService : IXxxService
        {
            public string GetName()
            {
                return "AService";
            }
        }
        
        public class XxxServiceRegistry : NbServiceRegistry<IXxxService, XxxServiceRegistry>
        {
            public override Func<IXxxService> GetDefaultServiceFunc()
            {
                return () => new AService();
            }
        }
        
        public class BService : IXxxService
        {
            public string GetName()
            {
                return "BService";
            }
        }
    }
}
